package com.wuchao.signer.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

class A {
    public void save() {
        System.out.println("----已经保存数据!----");
    }
}
class ProxyFactory implements MethodInterceptor {
    private A a;
    public ProxyFactory(A a){
        this.a=a;
    }
    //给目标对象创建一个代理对象
    public Object getProxyInstance(){
        Enhancer en=new Enhancer();
        en.setSuperclass(a.getClass());
        en.setCallback(this);
     return    en.create();//A'
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("前置增强");
        Object invoke = method.invoke(a, objects);
        System.out.println("后置增强");
        return invoke;

    }
}
public class Cglibtest {
    public static void main(String[] args) {
        ( (A) new ProxyFactory(new A()).getProxyInstance()).save();
    }
}
